package sm;

import java.text.DecimalFormat;

import org.apache.log4j.Logger;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.ProcCpu;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.ptql.ProcessFinder;

public class SMCPU extends Basic{
    ProcCpu pCpu = null;
    public SMCPU(){
       // pCpu = sigar.ProcCpu;
                //new ProcCpu();
    }
    
    public int getCPUCount() throws SigarException{
        return sigar.getCpuList().length;
    }
    public double getPidCPUUsage(long pid) throws SigarException {
        return  sigar.getProcCpu(pid).getPercent()/getCPUCount()*100;
    }
    public long[] getPids() throws SigarException{
        return sigar.getProcList();
    }
    public long[] getPidByName(String name) throws SigarException{
        ProcessFinder finder = new ProcessFinder(sigar);
        return finder.find("State.Name.eq="+name);
        
    }
    public long getPidByPort(long port) throws SigarException{
        return sigar.getProcPort(NetFlags.CONN_TCP, port);
    }
    public String getPidName(long pid) throws SigarException{
        return sigar.getProcState(pid).getName();
    }
    public String getPidCWD(long pid) throws SigarException{
        return sigar.getProcExe(pid).toString();
    }
    public String[] getArgsOfPid(long pid) throws SigarException{
        
        return sigar.getProcArgs(pid);
    }
    public String getArgsOfPidOneLine(long pid) throws SigarException{
        String[] argss=this.getArgsOfPid(pid);
        String arg="";
        for( String a:argss){
            arg+=a+" ";
        }
        return arg;
    }
    public String getPidCPUUsageAsString(long pid) throws SigarException{
        DecimalFormat  df1 = new DecimalFormat("##.##"); 
        return df1.format(this.getPidCPUUsage(pid));
    }
    public double getSystemCPUUsage() throws SigarException{
        return sigar.getCpuPerc().getCombined()*100;
    }
    public String getSystemCPUUsageAsString() throws SigarException{
        DecimalFormat  df1 = new DecimalFormat("##.##"); 
        return df1.format(sigar.getCpuPerc().getCombined()*100 );
    }
    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args){
        DecimalFormat  df1 = new DecimalFormat("##.##"); 
        Logger log = Logger.getLogger("cpu");
        SMCPU c = new SMCPU();
        long[] pids;
//        try {
//            //pids = c.getPidByName("chrome");
//            for( long pid: pids){
//                System.out.println("e: " + pid);
//            }
//        } catch (SigarException e1) {
//            // TODO Auto-generated catch block
//            e1.printStackTrace();
//        }
        
        
        
//        while(true){
            try {
                Thread.sleep(1000);
                log.info(df1.format(c.getPidCPUUsage(3540))+ "----- "+ df1.format(c.getSystemCPUUsage()));
//                log.info("system=" + c.getSystemCPUUsage()) ;
                log.info(c.getPidCWD(3540));
                
                String[] argss=c.getArgsOfPid(29348);
                String arg="";
                for( String a:argss){
                    arg+=a+" ";
                }
                log.info(arg);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
//    }
}
